package com.approval.approvalapp.index;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.HhpBean;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class HHListActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.xrv)
    XRecyclerView rv;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    @Bind(R.id.empty_tv)
    TextView empty_tv;
    private  int pagenum = 1;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    private String type;
    private boolean isProvince;
    private Adapter adapter;
    private List<HhpBean> hhpBeans = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_hhlist;
    }

    @Override
    public void initView() {
        type = getIntent().getStringExtra("type");
        if (type.equals("true")) {
            titleTv1.setText("红牌");
        } else {
            titleTv1.setText("黄牌");
        }
        isProvince = getIntent().getBooleanExtra("isProvince", false);

        rv.setPullRefreshEnabled(false);
        rv.setLayoutManager(new LinearLayoutManager(HHListActivity.this));
        adapter = new Adapter();
        rv.setAdapter(adapter);
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refresh();
            }
        },500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refresh();

                    }
                }, 500);
            }
        });
        rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                add();

            }
        });

        refresh();

    }

    private void refresh() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("region_code", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        jsonObject.put("isOverTime", type);
        jsonObject.put("page", pagenum);
        if (isProvince) {
            jsonObject.put("isProvince", "true");
        }
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/queryOverTimeList",
                jsonObject.toString(),
                new NetDownResponse() {

                    @Override
                    public void success(String str) {
                        hhpBeans = JSON.parseArray(str, HhpBean.class);
                        adapter.setHhpBean(hhpBeans, type);
                        if (hhpBeans == null || hhpBeans.size() == 0) {
                            empty_ll.setVisibility(View.VISIBLE);
                            if (type.equals("true")) {
                                empty_tv.setText("暂无红牌");
                            } else {
                                empty_tv.setText("暂无黄牌");
                            }
                        } else {
                            empty_ll.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {
                        if (swipeRefreshLayout != null){
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void finish() {
                        if (swipeRefreshLayout != null){
                            swipeRefreshLayout.setRefreshing(false);
                        }

                    }
                });
    }

    private void add() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("region_code", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        jsonObject.put("isOverTime", type);
        jsonObject.put("page", pagenum);
        if (isProvince) {
            jsonObject.put("isProvince", "true");
        }
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/queryOverTimeList",
                jsonObject.toString(),
                new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        List<HhpBean> mhhpBeans = JSON.parseArray(str, HhpBean.class);

                        if (mhhpBeans == null || mhhpBeans.size() == 0) {
                           rv.setNoMore(true);
                        } else {
                            for (int i = 0; i < mhhpBeans.size() ; i++) {
                                hhpBeans.add(mhhpBeans.get(i));
                            }
                            adapter.setHhpBean(hhpBeans,type);
                            rv.loadMoreComplete();
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {
                        ErrorBeanUtil.getErrorMessage(str);
                        if (rv != null){
                            rv.loadMoreComplete();
                        }
                    }

                    @Override
                    public void erro() {
                        ErrorBeanUtil.getErrorMessage(null);
                        if (rv != null){
                            rv.loadMoreComplete();
                        }
                    }

                    @Override
                    public void finish() {

                    }
                });
    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }


    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {

        List<HhpBean> hhpBean;
        String type;

        public void setHhpBean(List<HhpBean> hhpBean, String type) {
            this.hhpBean = hhpBean;
            this.type = type;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_hhp, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            if (type.equals("true")) {
                holder.iv.setBackgroundResource(R.drawable.red_pok);
            } else {
                holder.iv.setBackgroundResource(R.drawable.yellow_pok);
            }

            holder.sxmTv.setText("事项名: " + hhpBean.get(position).getTASKNAME());

            holder.sqrTv.setText("申请人: " + hhpBean.get(position).getAPPLYERNAME());
            long applydate = hhpBean.get(position).getAPPLYDATE();
            Date date = new Date(applydate);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            holder.sqsj_tv.setText("申请时间: " + format);

            holder.slbmTv.setText("受理部门: " + hhpBean.get(position).getITEMORGANNAME());

            holder.hjTv.setText("环节: " + hhpBean.get(position).getPROCESSNAME());

//            if (hhpBean.get(position).getPROMISETIMEUNIT().equals("G")) {
                holder.cnshTv.setText("承诺时长: " + hhpBean.get(position).getPROMISETIMELIMIT() + "个工作日");
//            } else {
//                holder.cnshTv.setText("承诺时长: " + 90 + "个自然日");
//            }
        }

        @Override
        public int getItemCount() {
            if (hhpBean == null) {
                return 0;
            } else {
                return hhpBean.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.iv)
            ImageView iv;
            @Bind(R.id.sxm_tv)
            TextView sxmTv;
            @Bind(R.id.sqr_tv)
            TextView sqrTv;
            @Bind(R.id.slbm_tv)
            TextView slbmTv;
            @Bind(R.id.hj_tv)
            TextView hjTv;
            @Bind(R.id.cnsh_tv)
            TextView cnshTv;
            @Bind(R.id.sqsj_tv)
            TextView sqsj_tv;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }
}
